using System;
using istIUniDB;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;

namespace Shraga.BLL
{
	/// <summary>
	/// Summary description for UniDBSingleton.
	/// </summary>
	public class UniDBSingleton
	{
		private string m_DBUsername = "Shraga";
		private string m_DBPassword = "Shraga";		
		private string m_DBServerName;
		
		private static UniDBSingleton m_singleton = null;
		private IUniDBClassFactory m_myUniDB = null;
		
		private UniDBSingleton()
		{						
			//						TcpChannel ch = new TcpChannel();
			//			ChannelServices.UnregisterChannel(ch);
			//						ChannelServices.RegisterChannel(ch);

#if DEBUG
			m_DBServerName = "istDev";
			m_myUniDB =  (istIUniDB.IUniDBClassFactory)
				Activator.GetObject(typeof(istIUniDB.IUniDBClassFactory),
				"tcp://istDev:1234/UniDBRemoting");
#elif RELEASE
			m_DBServerName = "istDev";
			m_myUniDB =  (istIUniDB.IUniDBClassFactory)
				Activator.GetObject(typeof(istIUniDB.IUniDBClassFactory),
				"tcp://istDev:1234/UniDBRemoting");
#elif ITUR
			m_DBServerName = "Shraga";
			m_myUniDB =  (istIUniDB.IUniDBClassFactory)
				Activator.GetObject(typeof(istIUniDB.IUniDBClassFactory),
				"tcp://istShraga:1234/UniDBRemoting");
#endif
		}

		private string ConnectionString
		{
			get
			{
				return "Password=" + m_DBPassword + ";User ID=" + m_DBUsername +
					";Data Source=" + m_DBServerName;
			}
		}

		public IUniDB UniDB
		{
			get { return m_myUniDB.GetUniDB(ConnectionString); }
		}

		public static UniDBSingleton Instance()
		{
			if (m_singleton == null)
				m_singleton = new UniDBSingleton();
			
			return m_singleton;
		}
	}
}
